# Build the docker image for onedev server
FROM ubuntu:22.04 AS build
ARG ARCH="amd64" PLATFORM="x86-64" BUILD_VERSION=9.3.0
ARG RELEASE_URL="${RELEASE_URL:-https://docker.lan.yunnan.ws/downloads/onedev-${BUILD_VERSION}.zip}"

COPY entrypoint-server.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y curl unzip apt-transport-https ca-certificates software-properties-common
RUN curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -
RUN add-apt-repository "deb [arch=${ARCH}] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
RUN apt-get -y update && apt-get -y install docker-ce
RUN curl -k -LO "${RELEASE_URL}" \
    && unzip onedev-${BUILD_VERSION}.zip && mv onedev-${BUILD_VERSION} /app \
    \
    # 创建/app/nboot目录并将需要的文件拷贝到目录
    && mkdir -p /app/nboot \
    && mv /app/boot/ch.qos.logback.logback-classic-1.2.11.jar /app/nboot/ \
    && mv /app/boot/ch.qos.logback.logback-core-1.2.11.jar /app/nboot/ \
    && mv /app/boot/io.onedev.commons-bootstrap-2.7.25.jar /app/nboot/ \
    && mv /app/boot/org.slf4j.jul-to-slf4j-1.7.36.jar /app/nboot/ \
    && mv /app/boot/org.slf4j.log4j-over-slf4j-1.7.36.jar /app/nboot/ \
    && mv /app/boot/org.slf4j.slf4j-api-1.7.36.jar /app/nboot/ \
    && mv /app/boot/libwrapper-linux-x86-64.so /app/nboot/ \
    && mv /app/boot/wrapper-linux-x86-64 /app/nboot/ \
    && mv /app/boot/wrapper.jar /app/nboot/ \
    # 清理用不上的文件
    && rm -rf /app/boot \
    # 将文件夹nboot修改为 boot
    && mv /app/nboot /app/boot \
    \
    && find /app -name "*.bat" -depth -exec rm {} \; \
    && rm -rf /app/3rdparty-licenses \
    && rm -rf /app/incompatibilities
# 安装最新版kubectl
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/${ARCH}/kubectl" && chmod +x kubectl && mv kubectl /usr/local/bin


# onedev server runtime container
FROM ubuntu:22.04

LABEL org.opencontainers.image.url=https://gitee.com/tekintian/onedev-server

ENV LANG=en_US.UTF-8 \
    LANGUAGE=en_US:en \
    LC_ALL=en_US.UTF-8
# copy file from builder container
COPY --from=build /usr/bin/docker /usr/local/bin/docker
COPY --from=build /app /app
COPY --from=build /entrypoint.sh /entrypoint.sh
COPY --from=build /usr/local/bin/kubectl /usr/local/bin/kubectl

RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y locales iproute2 curl git git-lfs openjdk-11-jre-headless \
    && rm -rf /var/lib/apt/lists/* \
    && sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
    && locale-gen

EXPOSE 6610 6611
CMD ["/entrypoint.sh"]